package com.ai.main;

import java.util.*;

import com.ai.check.InputCheck;
import com.ai.dao.*;
import com.ai.entity.*;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		MemberDao mdao = new MemberDao();
		CardDao cdao = new CardDao();
		InputCheck check = new InputCheck();
		while(true) {
			System.out.print(">iyoga ");
			String comand = sc.next();
			if(comand.equals("quit") || comand.equals("q")) {
				break;
			} else if(comand.equals("add-member")) {
				String name = sc.next();
				String address = sc.next();
				String job = sc.next();
				Member mem = new Member(name, address, job);
				if(mdao.addMember(mem) > 0) {
					int id = mdao.getId(mem);
					System.out.println("OK. " + name + "'s " + "member id is "+ id);
				} else {
					System.out.println("add member failed");
				}
				
			} else if(comand.equals("add-card")) {
				String name = sc.next();
				int id = mdao.getIdByName(name);
				String type = sc.next();
				int times = 0;
				MemberCard mc = null;
				
				if(type.equals("年卡")) {
					mc = new MemberCard(id, type);
				} else {
					times = sc.nextInt();
					if(check.checkTimes(times)) {
						mc = new MemberCard(id, type, times);
					} else { 
						System.out.println("times error! add card failed.");
						continue;
					}
				}
				
				if(cdao.addCard(mc) > 0) {
					if(type.equals("年卡")) {
						System.out.println("OK. " + "A new " + type + " added to " +
									name + "(" + id + ").");
					}
					else {
						System.out.println("OK. " + "A new " + type + " with " + times +
								" times added to " + name + ".");
					}
				} else {
					System.out.println("type error! add card failed.");
				}
				
			} else if(comand.equals("add-coach")) {
				CoachDao coachDao = new CoachDao();
				String name = sc.next();
				String family = sc.next();
				int age = sc.nextInt();
				String adv = sc.next();
				Coach coach = new Coach(name, family, age, adv);
				if(coachDao.addCoach(coach) > 0) {
					System.out.println("教练 【" + name + "】 添加成功.");
				} else {
					System.out.println("add coach failed.");
				}
			} else if(comand.equals("add-course")) {
				CourseDao csDao = new CourseDao();
				String name = sc.next();
				Course course = new Course(name);
				if(csDao.addCourse(course) > 0) {
					System.out.println("课程 【" + name + "】 添加成功.");
				} else {
					System.out.println("add course failed.");
				}
			} else if(comand.equals("schedule")) {
				ScheduleDao scheDao = new ScheduleDao();
				String time = sc.next();
				String course = sc.next();
				String coach = sc.next();
				Schedule sche = new Schedule(course, coach, time);
				if(scheDao.addSchedule(sche) > 0) {
					System.out.println("OK. 排课成功.");
				} else {
					System.out.println("schedule failed.");
				}
			} else if(comand.equals("subscribe")) {
				SubscpDao subDao = new SubscpDao();
				String time = sc.next();
				String name = sc.next();
				Subscription sub = new Subscription(name, time);
				if(subDao.subscribe(sub) > 0) {
					System.out.println("OK. " + name + "预定成功， 请于" + time + "准时到瑜伽馆" +
								"修炼XX老师的XXX");	
					// fixme:get coach and course from database
				} else {
					System.out.println("subscribe failed.");
				}
			} else if(comand.equals("cancel")) {
				SubscpDao subDao = new SubscpDao();
				String time = sc.next();
				String name = sc.next();
				Subscription sub = new Subscription(name, time);
				int status = subDao.cancel(sub);
				if(status > 0) {
					System.out.println("OK. 取消预定成功.");
				} else if(status < 0){
					System.out.println("Sorry, 取消预定课程， 必须在开课两小时之前");
				} else {
					System.out.println("cancel failed.");
				}
			} else {
				System.out.println("输入无效，请重新输入");
			}
		}
		sc.close();
	}	

}
